package com.ruoyi.user.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.pojo.request.PageSearchParam;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import com.ruoyi.system.api.model.user.AppUserChum;
import com.ruoyi.user.mapper.UserChumMapper;
import com.ruoyi.user.model.constant.UserConstant;
import com.ruoyi.user.model.pojo.entity.Chum;
import com.ruoyi.user.model.pojo.entity.UserChum;
import com.ruoyi.user.model.pojo.entity.UserChumApply;
import com.ruoyi.user.model.pojo.request.app.AppUserChumApplyRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumCheckRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumCreateRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumSearchRequest;
import com.ruoyi.user.model.pojo.result.app.AppUserChumInfoResult;
import com.ruoyi.user.model.pojo.result.app.UserBasicsInfo;
import com.ruoyi.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2024-12-24
 */
@Service
public class UserChumServiceImpl extends ServiceImpl<UserChumMapper, UserChum> implements IUserChumService {

    @Autowired
    private IBalanceService balanceService;
    @Autowired
    private IChumService iChumService;
    @Autowired
    private IUserChumApplyService iUserChumApplyService;
    @Autowired
    private RemoteAppUserService remoteAppUserService;
    @Autowired
    private UserChumMapper userChumMapper;
    @Autowired
    private IUserService iUserService;


    @Override
    public boolean openChum(AppUserChumCreateRequest request, Long userId) {
        Chum verify = iChumService.verify(request.getChumId());
        if (balanceService.docBalance(userId, UserConstant.incomeType.Zhi, "解锁密友位置:" + verify.getName(), UserConstant.BalanceType.balance, verify.getPrice())) {
            return this.openUserChum(userId, verify.getId(), verify.getName(), verify.getColorText());
        }
        return true;
    }

    @Override
    public boolean openUserChum(Long userId, Long chumId, String chumName, String chumColor) {
        UserChum userChum = new UserChum();
        userChum.setUserId(userId);
        userChum.setAimUserId(0L);
        userChum.setChumColorText(chumColor);
        userChum.setChumName(chumName);
        userChum.setChumId(chumId);
        userChum.setIntimacyNum(0);
        userChum.setCreateTime(new Date());
        return save(userChum);
    }

    @Override
    public List<UserChum> getUserChumList(Long userId, Long aimUserId) {
        return lambdaQuery().eq(!aimUserId.equals(0L), UserChum::getAimUserId, aimUserId)
                .eq(UserChum::getUserId, userId).list();
    }

    @Override
    public AppUserChum info(Long id) {
        UserChum userChum = verify(id);
        return AppUserChum.builder()
                .aimUserId(userChum.getAimUserId())
                .chumColorText(userChum.getChumColorText())
                .chumName(userChum.getChumName())
                .chumId(userChum.getChumId())
                .userId(userChum.getUserId())
                .id(userChum.getId())
                .build()
                ;
    }

    @Override
    public boolean apply(AppUserChumApplyRequest request, Long userId) {
        UserChum verify = verify(request.getUserChumId());
        R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
        AppUser data = userInfo.getData();

        Assert.isTrue(!verify.getUserId().equals(userId), "自己不能申请自己");
        Assert.isTrue(verify.getAimUserId().equals(0L), "此密友位置已有人");
        Chum chum = iChumService.verify(verify.getId());

        Long count = iUserChumApplyService.lambdaQuery()
                .eq(UserChumApply::getUserId, userId)
                .eq(UserChumApply::getAimUserId, verify.getUserId())
                .eq(UserChumApply::getStatus, UserConstant.AppStatus.no)
                .eq(UserChumApply::getUserChumId, request.getUserChumId())
                .count();
        Assert.isTrue(count == 0, "正在审核中请耐心等待");


        UserChumApply userChumApply = new UserChumApply();
        userChumApply.setAimUserId(verify.getUserId());
        userChumApply.setUserId(userId);
        userChumApply.setChumColorText(chum.getColorText());
        userChumApply.setChumName(chum.getName());
        userChumApply.setChumId(verify.getChumId());
        userChumApply.setStatus(UserConstant.AppStatus.no);
        userChumApply.setCreateTime(new Date());
        userChumApply.setUserChumId(request.getUserChumId());
        userChumApply.setUserNickName(data.getNickName());
        userChumApply.setUserPhoto(data.getPhoto());
        return iUserChumApplyService.save(userChumApply);
    }

    @Override
    public UserChum verify(Long id) {
        UserChum userChum = getById(id);
        Assert.notNull(userChum, "此密友不存在");
        return userChum;
    }

    @Override
    public Pages<UserChumApply> applyListPage(PageSearchParam pageSearchParam, Long userId) {
        Page<UserChumApply> userChumApplyPage = PageUtils.initPage(pageSearchParam, UserChumApply.class);
        Page<UserChumApply> page = iUserChumApplyService.lambdaQuery()
                .eq(UserChumApply::getAimUserId, userId)
                .eq(UserChumApply::getStatus, UserConstant.AppStatus.no)
                .page(userChumApplyPage);
        return PageUtils.pages(page);
    }

    @Override
    public boolean check(AppUserChumCheckRequest request, Long userId) {
        UserChumApply apply = iUserChumApplyService.getById(request.getId());
        Assert.notNull(apply, "密友申请记录不存在");
        Assert.isTrue(apply.getStatus().equals(UserConstant.AppStatus.no), "此记录已审核");
        Assert.isTrue(apply.getAimUserId().equals(userId), "您无法操作");
        UserChum userChum = getById(apply.getUserChumId());
        Assert.notNull(userChum, "您的这个密友位置不存在");
        Assert.isTrue(userChum.getAimUserId().equals(0L), "您的这个密友位置已被人占用");

        apply.setStatus(request.getStatus());
        iUserChumApplyService.updateById(apply);
        if (apply.getStatus().equals(UserConstant.AppStatus.yes)) {
            /*
            审核通过后增加密友关系
             */
            userChum.setAimUserId(apply.getUserId());
            userChum.setOpenTime(new Date());
            return updateById(userChum);
        }
        if (apply.getStatus().equals(UserConstant.AppStatus.notNo)) {
            /*
            审核拒绝后发送消息
             */
        }
        return true;
    }

    @Override
    public Pages<AppUserChumInfoResult> listPage(AppUserChumSearchRequest request) {
        Page<UserChum> userChumPage = PageUtils.initPage(request, UserChum.class);
        Page<AppUserChumInfoResult> resultIPage = userChumMapper.listPage(request, userChumPage);
        return PageUtils.pages(resultIPage);
    }

    @Override
    public AppUserChumInfoResult infoById(Long id) {
        UserChum verify = verify(id);
        UserBasicsInfo userBasicsInfo = iUserService.find(0L, verify.getAimUserId());
        return AppUserChumInfoResult.builder()
                .id(verify.getId())
                .aimUserId(verify.getAimUserId())
                .aimUserName(userBasicsInfo.getNickName())
                .aimUserPhoto(userBasicsInfo.getPhoto())
                .chumColorText(verify.getChumColorText())
                .chumId(verify.getChumId())
                .chumName(verify.getChumName())
                .intimacyNum(verify.getIntimacyNum())
                .openTime(DateUtils.timeByDay(new Date(), verify.getOpenTime()))
                .build()
                ;
    }

    @Override
    public boolean removeChum(AppUserChumApplyRequest request, Long userId) {
        UserChum verify = verify(request.getUserChumId());
        Assert.isTrue(verify.getUserId().equals(userId), "您无法操作");
        Assert.isTrue(!verify.getAimUserId().equals(0L), "当前密友不存在无法移除");
        verify.setAimUserId(0L);
        return updateById(verify);
    }

    @Override
    public boolean setChum(Long id, Long aimUserId) {
        UserChum verify = verify(id);
        Assert.isTrue(verify.getAimUserId().equals(0L), "此位置已被人占用");
        verify.setAimUserId(aimUserId);
        return updateById(verify);
    }

}
